//
//  StudentsViewController.m
//  SrnTeacher
//
//  Created by iDeneb on 6/18/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "StudentsViewController.h"
#import "SrnTeacherAppDelegate.h"
#import "SwitchViewController.h"
#import "user.h"
#import "QuartzCore/QuartzCore.h"

#define ROW_HEIGHT 60

@implementation StudentsViewController


@synthesize myscrollView,dateFormatter,viewData;
#pragma mark Table View Controller Methods;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	//udb =[db alloc];
	//BOOL re = [udb open];
	//currSid = [udb getMaxId] + 1;
	udb = [SrnTeacherAppDelegate App].udb;
	currSid = [SrnTeacherAppDelegate App].currid;
	self.view.backgroundColor = [UIColor lightGrayColor];
	myscrollView.layer.cornerRadius = 8;
	myscrollView.layer.masksToBounds = YES;
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (IBAction)done:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
    //if ([delegate respondsToSelector:@selector(setupViewController:didFinishSetupWithInfo:)]) {
    //    [delegate setupViewController:self didFinishSetupWithInfo:setupInfo];
    //}
}

-(void)updateResult:(User *)user
{	
	
	if([user.choice localizedCompare:@"raise"]==0){
		[[SrnTeacherAppDelegate App].listOfAllRaised addObject:user];
		[[[SrnTeacherAppDelegate App].viewController.handsRaisedViewController view] setNeedsDisplay];
	}
	else
		[[SrnTeacherAppDelegate App].listOfItems addObject:user];
	
	int i =0;
	for(; i< [[SrnTeacherAppDelegate App].listOfAllUsers count];i++)
	{
		User *tempUser = [[SrnTeacherAppDelegate App].listOfAllUsers objectAtIndex:i];
		if([tempUser.username localizedCompare:user.username] == 0)
		{
			break;
		}
	}
	
	if(i>= [[SrnTeacherAppDelegate App].listOfAllUsers count]){
		[[SrnTeacherAppDelegate App].listOfAllUsers addObject:user];
	}
	[myscrollView reloadData];
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if ([SrnTeacherAppDelegate App].currid == [SrnTeacherAppDelegate App].currVid  && [SrnTeacherAppDelegate App].isNew == YES) {
		return [[SrnTeacherAppDelegate App].listOfItems count];
	} else {
		int num = [SrnTeacherAppDelegate App].currVid;
		[viewData removeAllObjects];
		viewData = [udb getViewByNum: num];
		//for (int i =0 ; i < [tmp count]; i++)
		//[viewData addObject: [tmp objectAtIndex:i]];
		
		return [viewData count];
	}
		
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	//[cell release];
	//cell = nil;
	
	//if (cell == nil) {
		cell = [self tableViewCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
	//}
	
	// configureCell:cell forIndexPath: sets the text and image for the cell -- the method is factored out as it's also called during minuted-based updates.
	//[self configureCell:cell forIndexPath:indexPath];
    
    // Set up the cell...
	//User *cellValue = [[shouji138AppDelegate App].listOfItems objectAtIndex:indexPath.row];
	//cell.text = cellValue.username;
	
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -
#pragma mark Configuring table view cells

#define NAME_TAG 1
#define TIME_TAG 2
#define IMAGE_TAG 3

#define LEFT_COLUMN_OFFSET 10.0
#define LEFT_COLUMN_WIDTH 80.0

#define MIDDLE_COLUMN_OFFSET 110.0
#define MIDDLE_COLUMN_WIDTH 120.0

#define RIGHT_COLUMN_OFFSET 240.0

#define MAIN_FONT_SIZE 18.0
#define LABEL_HEIGHT 26.0

#define IMAGE_SIDE 30.0

- (UITableViewCell *)tableViewCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath{
	
	/*
	 Create an instance of UITableViewCell and add tagged subviews for the name, local time, and quarter image of the time zone.
	 */
	
	UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
	
	/*
	 Create labels for the text fields; set the highlight color so that when the cell is selected it changes appropriately.
	 */
	UILabel *label;
	CGRect rect;
	
	User *cellValue;
	if ([SrnTeacherAppDelegate App].currid == [SrnTeacherAppDelegate App].currVid  && [SrnTeacherAppDelegate App].isNew == YES)  {
		cellValue = [[SrnTeacherAppDelegate App].listOfItems objectAtIndex:indexPath.row];
	} else {
		
		cellValue = [viewData objectAtIndex:indexPath.row];	
	}	
	
	// Create a label for the time zone name.
	rect = CGRectMake(LEFT_COLUMN_OFFSET, (ROW_HEIGHT - LABEL_HEIGHT) / 2.0, LEFT_COLUMN_WIDTH, IMAGE_SIDE);
	//label = [[UILabel alloc] initWithFrame:rect];
	UIButton *viewButton = [[UIButton alloc] initWithFrame:rect];
	viewButton.tag = TIME_TAG;
	[viewButton setImage:[UIImage imageNamed:@"Show.png"] forState:UIControlStateNormal];	
	[viewButton addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
	[cell.contentView addSubview:viewButton];
	[viewButton release];
	
	rect = CGRectMake( MIDDLE_COLUMN_OFFSET, (ROW_HEIGHT - IMAGE_SIDE) / 2.0, 100, LABEL_HEIGHT);
	label = [[UILabel alloc] initWithFrame:rect];
	label.tag = NAME_TAG;
	label.font = [UIFont boldSystemFontOfSize:MAIN_FONT_SIZE];
	label.adjustsFontSizeToFitWidth = YES;
	[cell.contentView addSubview:label];
	label.highlightedTextColor = [UIColor whiteColor];
	label.text = cellValue.username;
	[viewButton setTitle:cellValue.username forState:UIControlStateNormal];	
	[label release];
	
	
	// Create an image view for the quarter image.
	rect = CGRectMake(RIGHT_COLUMN_OFFSET, (ROW_HEIGHT - IMAGE_SIDE) / 2.0, IMAGE_SIDE, IMAGE_SIDE);
	if(cellValue.image != nil){
		UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect];
		imageView.tag = IMAGE_TAG;
		[cell.contentView addSubview:imageView];
		imageView.image = cellValue.image;
		[imageView release];
	}else {
		label = [[UILabel alloc] initWithFrame:rect];
		label.tag = IMAGE_TAG;
		label.font = [UIFont boldSystemFontOfSize:MAIN_FONT_SIZE];
		label.adjustsFontSizeToFitWidth = YES;
		[cell.contentView addSubview:label];
		label.highlightedTextColor = [UIColor whiteColor];
		label.text = cellValue.choice;
		[label release];
		
	}
	if(cellValue.image != nil){
		UIImageView *imageView = (UIImageView *)[cell viewWithTag:IMAGE_TAG];
		
	}else {
		label = (UILabel *)[cell viewWithTag:IMAGE_TAG];
	
	}
	
	return cell;
}

  

-(IBAction)clickAction:(id)sender{
	UIButton* viewButton = (UIButton *)sender;
	NSLog(@"button view clicked!");
	NSString *uname = [viewButton currentTitle];
	[SrnTeacherAppDelegate App].uname = uname;
	[self.navigationController pushViewController:[SrnTeacherAppDelegate App].viewController.studentHistoryViewController animated:YES];
   [SrnTeacherAppDelegate App].viewController.studentsviewcontroller = self;
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	if(udb != nil)
	    [udb close];
}

- (void) viewWillAppear:(BOOL)animated{
	NSLog(@"student view will appear");
	[myscrollView reloadData];
	[self.view setNeedsDisplay];

}

- (void)dealloc {
    [super dealloc];
}



@end
